package Server.tool;

import Server.service.RedisService;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.springframework.http.HttpStatus;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SystemLogoutFilter extends LogoutFilter {
    private RedisService redisService;
    public SystemLogoutFilter(RedisService redisService){
        this.redisService=redisService;
    }
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse res) throws Exception {

        HttpServletResponse response=(HttpServletResponse)res;
        HttpServletRequest QErequest=(HttpServletRequest)request;
        response.setHeader("Access-control-Allow-Origin", QErequest.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,token");
        response.setHeader("X-Requested-With",QErequest.getHeader("X-Requested-With"));
        //防止乱码，适用于传输JSON数据
        //Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild
        response.setHeader("Content-Type","application/json;charset=UTF-8");
        response.setStatus(HttpStatus.OK.value());
        //redisService.deleted(QErequest.getHeader("X-real-ip"));

        if(WebUtils.isAjaxRequest(QErequest)){
            return false;
        }
        return super.preHandle(request, response);
    }
}
